<?php

class Admin_SistemaController extends App_Controller_Action
{
    private $_table     = 'SISTEMA';
    private $_pk        = 'SIS_ID';
    private $_order_col = 'SIS_ID';
    private $_order     = 'ASC';
    private $_sta_col   = '';
    private $obj        = '';
    private $form       = '';
    
    public function init()
    {
        $this->obj = new Admin_Model_DbTable_Sistema();
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Admin_Form_Sistema();
    }

     public function indexAction() {
         
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $vars = array('id'=>1);
        $this->_helper->redirector('edit', $this->_controller,$this->_module, $vars);
    }
    
    public function editAction() {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskMoney.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskedinput.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/mask.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');   
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_form_pdr.js');
        
        $this->view->form = $this->form;
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if(1){//$this->form->isValid($formData)) {
                    $id = 1;//(int) $formData[$this->_pk];//$this->form->getValue($this->_pk);
                    
                    $data = $formData;
                    unset($data[$this->_pk]);
                    unset($data['submit']);


                    //if ($id == 0){
                    //    $this->obj->insert($data);  
                    //}
                    //else{
                        $this->obj->update($data, $this->_pk.' = ' . $id);
                    //}
                        
                    $this->_helper->redirector('edit');
                } else {
                    $this->form->populate($formData);
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                // echo $ex->getMessage();
                //$this->form->populate($formData);
                $this->form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 1);
            if ($id > 0) {
                $this->form->populate($this->obj->fetchRow($this->_pk.' = ' . $id)->toArray());
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {   
            //$data = array($this->_sta_col => 0);
            //$ok = (bool) $this->obj->update($data, $this->_pk . ' = ' . $id);
            $ok = (bool) $this->obj->delete($this->_pk . ' = ' . $id);
        }       
        echo json_encode(array('ok' => $ok));
    }
}
